#!/bin/sh
#desc: Crea una ISO con todos los archivos necesarios para arrancar una MV

# Copyright(c) 2011 OpenDomo Services SL. Licensed under GPL v3 or later

# ----------------------------------------------------------------------------
#  Script Configuration
# ----------------------------------------------------------------------------
#RASPBERRY
#KERNEL=../../buildroot/output/images/bzImage
KERNEL=../../buildroot/output/images/kernel.img
ROOTFS=bootcd/initrd.gz
ISOFILES=../../isofiles
KEYMAPS=../../keymaps
WORK=/tmp
MNTDIR=/mnt/opendomo
INITRDSIZE=32

if test -z "$1"
then
	ISONAME="opendomo-unlabeled-486.iso"
else
	ODVER="$1"
	echo "----------------------------------------------"
	echo " Packaging release $ODVER!"
	echo "----------------------------------------------"
	sleep 1
	ISONAME="opendomo-$ODVER-486.iso"
fi

# If it does not exist, create
mkdir -p bootcd

# ----------------------------------------------------------------------------
#  Distro Files
# ----------------------------------------------------------------------------
#RASPBERRY
# Verify all file existence before continuing
#if test -f $KERNEL
#then
#	cp $KERNEL bootcd/linux
#else
#	echo "ERROR: $KERNEL not found !!"
#	exit 1
#fi
#if ! test -f "$ROOTFS"
#then
#	echo "ERROR: $ROOTFS not found!!"
#	exit 1
#fi
#if ! test -d $ISOFILES
#then
#	echo "ERROR: $ISOFILES not found!!"
#	wget www.opendomo.org/files/isofiles.tar.gz
#	tar -zxvf isofiles.tar.gz
#	mv isofiles $ISOFILES
#fi

#RASPBERRY
#cp -fr $ISOFILES/* bootcd/
# Copy also isofiles from SVN (if they exist!)
#cp isofiles/* bootcd/

# -- Information --
echo "Generating OpenDomo ISO file with following data:
   Kernel used: $KERNEL
   Initrd:      $ROOTFS
   Mount dir:   $MNTDIR"

#chmod 666 packages/*

ODCGI=`ls packages/odcgi*.tar.gz | head -n 1` 2>/dev/null
ODDISCOVERY=`ls packages/oddiscovery*.tar.gz | head -n 1` 2>/dev/null
ODCOMMON=`ls packages/odcommon*.tar.gz | head -n 1` 2> /dev/null
ODHAL=`ls packages/odhal*.tar.gz | head -n 1` 2> /dev/null
ODPKG=`ls packages/odpkg*.tar.gz | head -n 1` 2> /dev/null

if ! test -f "$ODCGI"; then
	echo "WARNING: odcgi not found. Fetching..."
	cp ../opendomo-cgi/pkg/odcgi*.tar.gz packages/ || exit 1
fi
if ! test -f "$ODDISCOVERY"; then
	echo "WARNING: oddiscovery not found. Fetching..."
	cp ../opendomo-discovery/pkg/oddiscovery*.tar.gz packages/ || exit 1
fi
if ! test -f "$ODCOMMON"; then
	echo "WARNING: odcommon not found. Fetching..."
	cp ../opendomo-common/pkg/odcommon*.tar.gz packages/ || exit 1
fi
if ! test -f "$ODHAL"; then
	echo "WARNING: odhal not found. Fetching..."
	cp ../opendomo-hal/pkg/odhal*.tar.gz packages/ || exit 1
fi
if ! test -f "$ODPKG"; then
	echo "WARNING: odpkg not found. Fetching..."
	cp ../opendomo-pkg/pkg/odpkg*.tar.gz packages/ || exit 1
fi

# Some extra packages
cp ../opendomo-vision/pkg/od*gz packages/ 2>/dev/null
cp ../opendomo-events/pkg/od*gz packages/ 2>/dev/null

if test `ls packages/*.gz | wc -l` -ne `ls packages/*.gz | cut -f 1 -d'-' | uniq | wc -l`; then
	echo "ERROR: Repeated package name"
	exit 1
fi
echo


#RASPBERRY
# With this we keep the CD mounted in /mnt/opendomodistro after booting
#echo 'LABEL="opendomodistro"' > bootcd/opendomo.cfg
#echo 'CONFDEVICE="1"' >> bootcd/opendomo.cfg
#echo 'SYSDEVICE="1"' >> bootcd/opendomo.cfg

# Data files
#RASPBERRY
#mkdir -p bootcd/data
mkdir -p $MNTDIR/data
#cp data/* bootcd/data/
cp data/* $MNTDIR/data/

# Package cache
#RASPBERRY
#mkdir -p bootcd/pkgcache
mkdir -p $MNTDIR/mnt/odconf/pkgcache
#rm -fr bootcd/pkgcache/*
rm -fr $MNTDIR/mnt/odconf/pkgcache/*
#cp packages/*.tar.gz bootcd/pkgcache/
cp packages/*.tar.gz $MNTDIR/mnt/odconf/pkgcache/

#RASPBERRY
#cd bootcd/pkgcache
cd $MNTDIR/mnt/odconf/pkgcache
echo "Packages included:"
for i in *.tar.gz  
do
	BN=`basename $i | sed 's/\.tar\.gz//'`
	echo "   $BN"
done

cd ../..


USR="--owner 1000 --group 1000 "
EXCLUDE=" --exclude '*~' --exclude .svn "
#RASPBERRY
pwd
cd $MNTDIR/mnt/odconf/sysconf
#cd $MNTDIR
if ! test -z "$ODVER"; then
	touch etc/opendomo/repositories.cfg
	chown 1000 etc/opendomo/repositories.cfg
	echo "http://es.opendomo.org/files/pkg/" > etc/opendomo/repositories.cfg
	echo "http://es.opendomo.org/files/pkg/$ODVER/" >> etc/opendomo/repositories.cfg
	#RASPBERRY
	#echo $ODVER > ../bootcd/VERSION
	echo $ODVER > $MNTDIR/VERSION
fi
#RASPBERRY
#mkdir -p ../bootcd/sysconf
mkdir -p $MNTDIR/mnt/odconf/sysconf  
#tar -zcf ../bootcd/sysconf/sysconf.tar.gz * $USR $EXCLUDE
tar -zcf $MNTDIR/mnt/odconf/sysconf/sysconf.tar.gz * $USR $EXCLUDE
cd ..


# ----------------------------------------------------------------------------
#  Make ISO
# ----------------------------------------------------------------------------
#RASPBERRY
#if mkisofs -o ../$ISONAME -b isolinux.bin -c boot.cat -no-emul-boot \
#	-boot-load-size 4 -boot-info-table -J -input-charset utf-8 \
#	-hide-rr-moved -R bootcd/ 2>/dev/null
#then
	echo "Finished:"
#	ls -lah ../$ISONAME
#fi


